<?php

namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller{
    
public function index(){
      $category=D('category')->getTree(0,'id,title,pid');//一级分类查询结果
      // dump($category);
      $hot=M('good')->where(['hot'=>0])->limit(8)->select();//查询good表中hot=0热卖中商品加上limit查询限制只查询8条
      $up=M('good')->where(['up'=>0])->limit(8)->select();
      // dump($hot);die;
      $price = M('attr')->getfield('gid,price');
      $this->assign('price',$price);
      $this->assign('up',$up);
      $this->assign('hot',$hot);
      $this->assign('category',$category);
      $this->display();


}


public function hotjuti($id){
     $id=I('get.id');
     $hotjuti=M('good')->where(['id'=>$id])->select();
     // var_dump($hotjuti);//查询热销商品具体一个详情页
     $cate=$this->get_parent_category($hotjuti['sort']);
     // var_dump($cate);die;
     $price=M('attr')->getfield('gid,price');
     $num=M('attr')->getfield('gid,num');

     $attrkey=M('attrkey')->where(['gid'=>$id])->select();//查询点击进入详情商品的几种属性类
     $attrval=M('attrval')->where(['gid'=>$id])->select();//查询点击进入详情商品的几种属性值
     foreach($attrkey as &$v){
     	foreach($attrval as $vv){
     		if($v['id']==$vv['kid']){
     			$v['attr_key'][]= $vv;
     		}
     	}
     }
     // dump($attrkey);
     // var_dump($attrkey,$attrval);die;
     $this->assign('num',$num);
     $this->assign('price',$price);
     $this->assign('hotjuti',$hotjuti);
     $this->assign('attrkey',$attrkey);
     $this->display();


     

}
   


   public function detailPrice(){

     $where['gid'] =I('post.gid');
     $where['attrvid']=implode(',',I('post.attrvid'));
     
     $res= M('attr')->where($where)->find();
     $this->ajaxReturn($res);

   }




  public function get_parent_category($cid){
    if(empty($cid)){
        return false;
    }
    $cates  =   M('Category')->where(array('status'=>1))->field('id,title,pid')->order('sort')->select();
    $child  =   get_category($cid); //获取参数分类的信息
    $pid    =   $child['pid'];
    $temp   =   array();
    $res[]  =   $child;
    while(true){
        foreach ($cates as $key=>$cate){
            if($cate['id'] == $pid){
                $pid = $cate['pid'];
                array_unshift($res, $cate); //将父分类插入到数组第一个元素前
            }
        }
        if($pid == 0){
            break;
        }
    }
    return $res;
}

}